package com.syxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.exception.DIYException;
import com.syxd.common.exception.ErrorCode;
import com.syxd.common.service.impl.CrudServiceImpl;
import com.syxd.common.utils.BeanHelper;
import com.syxd.dao.SysConfigDao;
import com.syxd.dao.SysSunCreditContactDao;
import com.syxd.dao.SysSunCreditDao;
import com.syxd.dto.SysSunCreditContactDto;
import com.syxd.dto.SysSunCreditDto;
import com.syxd.entity.SysSettingEntity;
import com.syxd.entity.SysSunCreditContactEntity;
import com.syxd.entity.SysSunCreditEntity;
import com.syxd.service.SysSunCreditContactService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class SysSunCreditContactServiceImpl extends CrudServiceImpl<SysSunCreditContactDao, SysSunCreditContactEntity, SysSunCreditContactDto> implements SysSunCreditContactService {
    @Autowired
    SysSunCreditContactDao sysSunCreditContactDao;
    @Autowired
    SysSunCreditDao sysSunCreditDao;
    @Autowired
    SysUserServiceImpl sysUserService;
    @Autowired
    SysConfigDao sysConfigDao;

    @Override
    public WebResult delSunCreditContact(Long id, HttpServletRequest request) {

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("is_delete", Constant.FDELETE);
        SysSunCreditEntity sysSunCreditEntity = sysSunCreditDao.selectOne(queryWrapper);
        SysSettingEntity sysSettingEntity = sysConfigDao.selectById(1);
        if (sysSunCreditEntity.getUpdateTime() == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysSunCreditEntity.getCreateTime());
            calendar.add(calendar.DATE, sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis() < time.getTime()) {
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysSunCreditEntity.getUpdateTime());
            calendar.add(calendar.DATE, sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis() < time.getTime()) {
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }

        }
        SysSunCreditContactEntity sysSunCreditContactEntity = BeanHelper.copyProperties(sysSunCreditEntity, SysSunCreditContactEntity.class);
        sysSunCreditContactEntity.setSourceDataId(id.intValue());
        sysSunCreditContactEntity.setCreateTime(new Date());
        sysSunCreditContactEntity.setOperationType(Constant.operationType.delete.getValue());
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.wait.getValue());
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setApplicantId(userId);
        sysSunCreditContactEntity.setUpdateTime(null);
        sysSunCreditContactDao.insert(sysSunCreditContactEntity);
        return new WebResult();
    }

    @Override
    public WebResult update(SysSunCreditDto dto, HttpServletRequest request) {
        SysSunCreditContactEntity sysSunCreditContactEntity = BeanHelper.copyProperties(dto, SysSunCreditContactEntity.class);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dto.getId());
        queryWrapper.eq("is_delete", Constant.FDELETE);
        SysSunCreditEntity sysSunCreditEntity = sysSunCreditDao.selectOne(queryWrapper);
        SysSettingEntity sysSettingEntity = sysConfigDao.selectById(1);
        if (sysSunCreditEntity.getUpdateTime() == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysSunCreditEntity.getCreateTime());
            calendar.add(calendar.DATE, sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis() < time.getTime()) {
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysSunCreditEntity.getUpdateTime());
            calendar.add(calendar.DATE, sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis() < time.getTime()) {
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }

        }
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setSourceDataId(dto.getId().intValue());
        sysSunCreditContactEntity.setApplicantId(userId);
        sysSunCreditContactEntity.setOperationType(Constant.operationType.update.getValue());
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.wait.getValue());
        sysSunCreditContactEntity.setCreateTime(new Date());
        sysSunCreditContactEntity.setUpdateTime(null);
        sysSunCreditContactDao.insert(sysSunCreditContactEntity);
        return new WebResult();
    }

    @Override
    public WebResult put(SysSunCreditDto dto, HttpServletRequest request) {
        //新增业务判断是否存在,将户主身份证Id设为唯一
        if (dto.getHouseholderIdcard() != null) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("householder_idcard", dto.getHouseholderIdcard());
            SysSunCreditEntity sysSunCreditEntity = sysSunCreditDao.selectOne(queryWrapper);
            if (sysSunCreditEntity != null) {
                return new WebResult().error(ErrorCode.household_already_exists);
            }
        }
        SysSunCreditContactEntity sysSunCreditContactEntity = BeanHelper.copyProperties(dto, SysSunCreditContactEntity.class);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setApplicantId(userId);
        sysSunCreditContactEntity.setOperationType(Constant.operationType.add.getValue());
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.wait.getValue());
        sysSunCreditContactEntity.setCreateTime(new Date());
        sysSunCreditContactEntity.setUpdateTime(null);
        sysSunCreditContactDao.insert(sysSunCreditContactEntity);
        return new WebResult();
    }


    @Override
    public QueryWrapper<SysSunCreditContactEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<SysSunCreditContactEntity> wrapper = new QueryWrapper<>();
        String branchName = (String) params.get("branchName");
        String householderIdcard = (String) params.get("householderIdcard");
        String householderPhone = (String) params.get("householderPhone");
        String spouseName = (String) params.get("spouseName");
        String spouseIdcard = (String) params.get("spouseIdcard");
        String spousePhone = (String) params.get("spousePhone");
        String householderName = (String) params.get("householderName");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        if (StringUtils.isNotBlank(endDate)) {
            endDate = endDate + " 23:59:59";
        }
        String processingStatus = (String) params.get("processingStatus");

        if(StringUtils.isNotBlank(processingStatus)){
            wrapper.eq( "processing_status", processingStatus);
        }else {
            wrapper.eq("processing_status", 1);
        }

        wrapper.eq(StringUtils.isNotBlank(branchName), "branch_name", branchName);
        wrapper.eq(StringUtils.isNotBlank(householderIdcard), "householder_idcard", householderIdcard);
        wrapper.eq(StringUtils.isNotBlank(householderPhone), "householder_phone", householderPhone);
        wrapper.eq(StringUtils.isNotBlank(spouseName), "spouse_name", spouseName);
        wrapper.eq(StringUtils.isNotBlank(spouseIdcard), "spouse_idcard", spouseIdcard);
        wrapper.eq(StringUtils.isNotBlank(spousePhone), "spouse_phone", spousePhone);
        wrapper.eq(StringUtils.isNotBlank(householderName), "householder_name", householderName);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_time", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_time", endDate);
        wrapper.eq("is_delete", Constant.FDELETE);
        return wrapper;
    }
}

